//
//  HMEmoticonToolBar.swift
//  EmoticonKeyboard
//
//  Created by 周英培 on 16/5/30.
//  Copyright © 2016年 周英培. All rights reserved.
//

/*
 1.UIStackView是iOS9.0推出的 要想兼容以前的 需要下载第三方框架FDStackView
 2.//UIStackView添加子视图 不能够使用addSubview
 addArrangedSubview(btn)
 3.//设置轴
 axis = .Horizontal
 
 4.//设置填充方式
 distribution = .FillEqually
 */
import UIKit

private let baseTag = 1000

class HMEmoticonToolBar: UIStackView {
    
    //记录按钮的点击状态
    var currentBtn : UIButton?
    
    //定义一个闭包 实现获取toolbar的三个按钮的点击哪个被点击
    var selectedIndexBtnClick : ((index:Int) -> ())?
    

    init() {
    
        super.init(frame: CGRectZero)
        self.backgroundColor = UIColor.blueColor()
        
        //设置轴
        axis = .Horizontal
        
        //设置填充方式
        distribution = .FillEqually
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //MARK:设置UI
    private func setupUI() {
        
        //获取组模型数据
        let packages = HMEmoticonManager.sharedEmoticonTools.packages
        
        for i in 0..<packages.count {
            
            let package = packages[i]
            
            var backImageName = "compose_emotion_table_mid"
            
            if i == 0 {
                
                backImageName = "compose_emotion_table_left"
                
            }else if i == packages.count - 1 {
            
                backImageName = "compose_emotion_table_right"
            
            }
            
            addChildButtons(package.title!, backImageName: backImageName, index: i)
            
        }
        
//        addChildButtons("默认", backImageName: "compose_emotion_table_left", index: 0)
//        addChildButtons("Emoji", backImageName: "compose_emotion_table_mid",index: 1)
//        addChildButtons("浪小花", backImageName: "compose_emotion_table_right", index: 2)
        
    }
    
    
    private func addChildButtons(title: String, backImageName: String, index: Int) {
        let btn = UIButton()
        //设置按钮的tag
        btn.tag = index + baseTag
        btn.setTitle(title, forState: .Normal)
        btn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Selected)
       
        //添加按钮的点击事件
        btn.addTarget(self, action: #selector(btnClick(_:)), forControlEvents: .TouchUpInside)
        
        btn.setBackgroundImage(UIImage(named: backImageName + "_normal"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: backImageName + "_selected"), forState: .Selected)
        //UIStackView添加子视图 不能够使用addSubview
        addArrangedSubview(btn)
        
        //默认选中第一个按钮
        if index == 0 {
            btn.selected = true
            
            currentBtn = btn
        }
        
    }
    
    @objc private func btnClick(btn:UIButton) {
    
    print("按钮被点击")
        
        //取消之前的按钮点击状态
        currentBtn?.selected = false
        
        btn.selected = true
        
        //记录按钮的点击状态
        currentBtn = btn
        
        //执行闭包完成回调
        selectedIndexBtnClick?(index: btn.tag - baseTag)
    
    }
   
//下面写的代码具体的意思不懂？？？
    //toolbar三个按钮的点击根表情相连的设置
    func setSelectBtn(section:Int) {
    
        let btnTag = baseTag + section
        
        let btn = self.viewWithTag(btnTag) as! UIButton
        
        currentBtn?.selected = false
        
        btn.selected = true
        
        currentBtn = btn
    
    
    }
}
